package com.hongxun.design.adapters;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.List;

import com.hongxun.design.R;
import com.hongxun.design.activitys.CreateProjectActivity;
import com.hongxun.design.activitys.CreateProjectAttributeActivity;
import com.hongxun.design.activitys.ProjectSearchActivity;
import com.hongxun.design.activitys.StatisticsSearchActivity;
import com.hongxun.design.bean.UnitBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;

/**
 * Created by zerdoor_pc on 2016/5/19.
 */
public class CreateRADIOAdapter extends BaseAdapter {
    List<UnitBean> itemBean;
    Context context;
    LayoutInflater inflater;

    public CreateRADIOAdapter(List<UnitBean> itemBean, Context context) {
        this.itemBean = itemBean;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemBean.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        UnitBean bean = itemBean.get(i);
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_createattributecodeset, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.textView.setText(bean.getDepartname());
        holder.textView.setOnClickListener(new MyOnclickListener(bean) {
            @Override
            public void onClick(View view, UnitBean bean) {
                if ( ((CreateProjectAttributeActivity) context).Code.equals("1")){
                    if (CreateProjectAttributeActivity.TYPENUM.length()>0){
                        ProjectSearchActivity.adapter.setValue(Integer.parseInt(CreateProjectAttributeActivity.TYPENUM),bean);
                    }
                }else  if(((CreateProjectAttributeActivity)context).Code.equals("StatisticsSearchActivity")){
                    if (CreateProjectAttributeActivity.TYPENUM.length()>0){
                        StatisticsSearchActivity.adapter.setValue(Integer.parseInt(CreateProjectAttributeActivity.TYPENUM),bean);
                    }
                }else{
                    CreateProjectActivity.setValue(Constant.ACTIVITYRADIO, bean,CreateProjectAttributeActivity.TYPENUM);
                }

                ((Activity) context).finish();

            }
        });
        return view;
    }

    abstract class MyOnclickListener implements View.OnClickListener {
        UnitBean bean;

        MyOnclickListener(UnitBean bean) {
            this.bean = bean;
        }

        @Override
        public void onClick(View view) {
            onClick(view, bean);
        }

        public abstract void onClick(View view, UnitBean bean);
    }

    class Holder {
        TextView textView, right;

        Holder(View view) {
            textView = (TextView) view.findViewById(R.id.codeset_version);
            right = (TextView) view.findViewById(R.id.codeset_version_right);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
            right.setVisibility(View.GONE);
        }
    }
}
